package com.zzty.zzlp.serviceimpl;

import com.zzty.zzlp.commom.PropertyUtil;
import com.zzty.zzlp.dao.ChatMapper;
import com.zzty.zzlp.dao.GroupMapper;
import com.zzty.zzlp.dao.UserDao;
import com.zzty.zzlp.entity.*;
import com.zzty.zzlp.service.ChatService;
import com.zzty.zzlp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzty.zzlp.service.GroupService;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    GroupMapper groupMapper;

    @Autowired
    ChatMapper chatMapper;

    @Autowired
    UserService userService;

    public void createGroup(Group group){
        groupMapper.createGroup(group);
    }

    public Integer searchNextGroupId(){
        if(groupMapper.searchMaxGroupId() == null){
            return 100001;
        }
        return groupMapper.searchMaxGroupId()+1;
    }

    public void setGroupInfo(Integer id,String name){
        groupMapper.setGroupInfo(id,name);
    }

    public GroupInfo searchGroupName(Integer id){
        return groupMapper.searchGroupName(id);
    }

    public List<Group> searchAllGroup(Integer userId){
        return groupMapper.searchAllGroup(userId);
    }

    public List<Integer> searchUsersInGroup(Integer groupId){
        return groupMapper.searchUsersInGroup(groupId);
    }

    public Chat searchGroupLastMsg(Integer groupId){
        Chat chat = chatMapper.searchGroupLastMsg(groupId);
        if(chat == null){
            Chat tempChat = new Chat();
            tempChat.setType("text");
            tempChat.setMsg("");
            return tempChat;
        }else{
            return chat;
        }
    }

    public List<ChatBuffer> searchGroupMessageCount(Integer groupId, Integer userId){
        List<ChatBuffer> chatList = chatMapper.searchGroupMessageCount(groupId,userId);
        for(int i=0;i<chatList.size();i++){
            if((chatList.get(i).getMsg().endsWith(".doc")) || (chatList.get(i).getMsg().endsWith(".docx"))){
                chatList.get(i).setIcon("/zzlp/img/word.jpg");
            }else if (chatList.get(i).getMsg().endsWith(".xls")) {
                chatList.get(i).setIcon("/zzlp/img/excel.jpg");
            } else if (chatList.get(i).getMsg().endsWith(".txt")) {
                chatList.get(i).setIcon("/zzlp/img/txt.jpg");
            } else if ((chatList.get(i).getMsg().endsWith(".zip")) || (chatList.get(i).getMsg().endsWith(".rar"))) {
                chatList.get(i).setIcon("/zzlp/img/zip.jpg");
            }
        }
        return chatList;
    }

    public void readGroupMessage(Integer groupId, Integer userId){
        chatMapper.readGroupMessage(groupId,userId);
    }

    public List<User_info> searchUserInfos(){
        return groupMapper.searchUserInfos();
    }

    public String searchAllGroupMsg(Integer groupId,Integer userId){
        List<Chat> chatList =  groupMapper.searchAllGroupMsg(groupId,userId);
        String path = createTXT(userId, chatList);
        System.out.println("path:" + path);
        return path;
    }

    public String createTXT(Integer userId,  List<Chat> chatList) {
        Date date = new Date();
        String path = PropertyUtil.prop.getProperty("path.chatLog") + date.getTime() + userId + ".txt";
        try {
            File file = new File(path);
            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            for(Chat tempChat : chatList){
                if("file".equals(tempChat.getType())){
                    tempChat.setMsg("[文件]");
                }else if("img".equals(tempChat.getType())){
                    tempChat.setMsg("[图片]");
                }else{
                    Pattern pattern = Pattern.compile("^_[0-9a-zA-Z]{4}");
                    Matcher matcher = pattern.matcher(tempChat.getMsg());
                    while(matcher.find()){
                        String e=matcher.group(0);
                        tempChat.setMsg(tempChat.getMsg().replaceAll(e,"[表情]"));
                    }
                }
                bw.write(userService.selectUserById(tempChat.getFromId()).getName() +
                        "\t" + tempChat.getTime() + "\r\n" + tempChat.getMsg() + "\r\n");
            }
            bw.close();
            System.out.println("Done");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }
}
